2
2
.
.
4
4
.
.
1
1
2
2
D
D
o
o
w
w
n
n
l
l
o
o
a
a
d
d
-
-
E
E
x
x
c
c
e
e
l
l
-
-
W
W
i
i
t
t
h
h
H
H
e
e
a
a
d
d
e
e
r
r
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to use ResponseEntity to download Excel from the Controller.
And how to use first column as Header. (to specify Column Names)
You can either call
● http://localhost:8080/DownloadExcel to directly download Excel (Page Content is not changed)
● http://localhost:8080/GetExcel to display download link
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller, @RequestMapping and Tomcat HTTP Server.
http://localhost:8080/GetExcel
Tomcat
getExcel()
MyController
Downloads Test.xlsx
downloadExcel()
http://localhost:8080/DownloadExcel
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_endpoint_return_excel_header (add Spring Boot Starters from the table)
Edit File: pom.xml (add poi Maven dependencies)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside controllers package)
pom.xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
MyController.java
package com.ivoronline.springboot_endpoint_return_excel_header.controllers;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
@Controller
public class MyController {
//=======================================================================
// GET EXCEL
//=======================================================================
@ResponseBody
@GetMapping("/GetExcel")
public String getExcel() {
return "<a href='DownloadExcel'> Download Excel </a>";
}
//=======================================================================
// DOWNLOAD EXCEL
//=======================================================================
@ResponseBody
@GetMapping("/DownloadExcel")
public ResponseEntity<StreamingResponseBody> DownloadExcel() {
//CREATE EXCEL FILE
Workbook workBook = new XSSFWorkbook();
//CREATE TABLE
Sheet sheet = workBook.createSheet("My Sheet");
sheet.setColumnWidth(0, 10 * 256); //10 characters wide
//CREATE HEADER
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("NAME");
header.createCell(1).setCellValue("AGE");
//CREATE ROW 1
Row row1 = sheet.createRow(1);
row1.createCell(0).setCellValue("John");
row1.createCell(1).setCellValue("20");
//CREATE ROW 2
Row row2 = sheet.createRow(2);
row2.createCell(0).setCellValue("Bill");
row2.createCell(1).setCellValue("50");
//DOWNLOAD EXCEL
return ResponseEntity
.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_DISPOSITION, "inline;filename=\"People.xlsx\"")
.body(workBook::write);
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/GetExcel
Test.xslx
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
</dependencies>